import request from "@/api";
import { BaseRet } from "../response";

export default class DesignTableApi {
  /**
   * 查询表
   * @returns
   */
  static async query(): Promise<DesignTableModel[]> {
    return await request({
      url: "/designTable/query",
      method: "get",
    });
  }

  /**
   * 新增表
   * @returns
   */
  static async create(data: DesignTableEdit): Promise<BaseRet> {
    return await request({
      url: `/designTable/create`,
      method: "post",
      data: data,
    });
  }

  /**
   * 修改表
   * @returns
   */
  static async update(data: DesignTableEdit): Promise<BaseRet> {
    return await request({
      url: `/designTable/update`,
      method: "post",
      data: data,
    });
  }

  /**
   * 删除表
   * @returns
   */
  static async delete(tableName: string): Promise<BaseRet> {
    return await request({
      url: `/designTable/delete/${tableName}`,
      method: "delete",
    });
  }

  /**
   * 查询字段
   * @returns
   */
  static async queryColumn(tableName: string): Promise<DesignColumnModel[]> {
    return await request({
      url: `/designTable/queryColumn/${tableName}`,
      method: "get",
    });
  }

  /**
   * 查询字段类型下拉框
   * @returns
   */
  static async queryDataTypeSelect(): Promise<DesignColumnDataType[]> {
    return await request({
      url: `/designTable/QueryDataTypeSelect`,
      method: "get",
    });
  }

  /**
   * 修改表名称
   * @returns
   */
  static async tableRename(data: DesignTableRename): Promise<BaseRet> {
    return await request({
      url: `/designTable/TableRename`,
      method: "put",
      data: data,
    });
  }
}

export interface DesignTableModel {
  name?: string;
  description?: string;
}

export interface DesignTableRename {
  oldTableName?: string;
  newTableName?: string;
}

export interface DesignTableEdit {
  name?: string;
  description?: string;
  columnList?: DesignColumnModel[];
}

export interface DesignColumnModel {
  columnName?: string;
  oldColumnName?: string;
  dataType?: string;
  length?: number;
  decimalDigits?: number;
  columnDescription?: string;
  isNullable?: boolean;
  isPrimarykey?: boolean;
}

export interface DesignColumnDataType {
  code?: string;
  type?: string;
  description?: string;
}
